package com.gzsxy.JUC.bq;

import java.util.Collection;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * blockingQueue阻塞队列
 */
public class Test {
    public static void main(String[] args) throws InterruptedException {
        //collection
        //list
        //set
        //
//        Collection
        test3();
    }


    /**
     * 抛出异常
     */
    public static void test1() {
        //队列的大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(blockingQueue.add('a'));  //添加队列元素
        System.out.println(blockingQueue.add('b'));
        System.out.println(blockingQueue.add('c'));
//        System.out.println(blockingQueue.add('c'));  //抛出异常

        System.out.println(blockingQueue.element()); //查看队首元素是谁

        System.out.println(blockingQueue.remove()); //移除队列元素
        System.out.println(blockingQueue.remove());
        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove()); //抛出异常
    }


    /**
     * 有返回值，不跑出异常
     */
    public static void test2() {
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);


        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d")); //返回false 不抛出异常


        System.out.println("=========================");
        System.out.println(blockingQueue.peek());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());//返回null 不抛出异常
    }

    /**
     * 等待，阻塞 （一直等待）
     *
     * @throws InterruptedException
     */
    public static void test3() throws InterruptedException {
        //队列大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(5);

        //一直阻塞
        blockingQueue.put("a");
        blockingQueue.put("b");
        blockingQueue.put("c");
        blockingQueue.put("d"); //队列没有位置，一直阻塞
        blockingQueue.put("d"); //队列没有位置，一直阻塞
        blockingQueue.put("d"); //队列没有位置，一直阻塞
        blockingQueue.put("d"); //队列没有位置，一直阻塞
        blockingQueue.put("d"); //队列没有位置，一直阻塞
        blockingQueue.put("d"); //队列没有位置，一直阻塞
        blockingQueue.put("d"); //队列没有位置，一直阻塞


        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.take());  //没有这个元素
        System.out.println(blockingQueue.take());  //没有这个元素
        System.out.println(blockingQueue.take());  //没有这个元素
        System.out.println(blockingQueue.take());  //没有这个元素
        System.out.println(blockingQueue.take());  //没有这个元素
        System.out.println(blockingQueue.take());  //没有这个元素
    }


    /**
     * 超时等待
     *
     * @throws InterruptedException
     */
    public static void test4() throws InterruptedException {
        //队列大小
        ArrayBlockingQueue blockingQueue = new ArrayBlockingQueue<>(3);

        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("d", 2, TimeUnit.SECONDS)); //超时2s后退出

        System.out.println("========");

        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll(2, TimeUnit.SECONDS)); //超时2s后退出

    }

}
